package com.jingyanzi.sys.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

import com.jingyanzi.sys.domain.service.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.jingyanzi.sys.domain.service.IAddressService;
import com.jingyanzi.sys.domain.service.IBusinessService;
import com.jingyanzi.sys.domain.service.IFeedbackService;
import com.jingyanzi.sys.domain.service.IMemberConfigService;
import com.jingyanzi.sys.domain.service.INoticeService;
import com.jingyanzi.sys.domain.service.IOperateLogsService;


public class SysFactory {

	private static AtomicBoolean isInit = new AtomicBoolean(false);
	
	private static ApplicationContext context = null;
	
	private static AtomicBoolean isTest = new AtomicBoolean(false);
	
	public static void initCofing() {
		Properties properties = new Properties();
		InputStream in = SysFactory.class.getClassLoader().getResourceAsStream("application.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String value = properties.getProperty("run.test");
		if(value.equals("on")) {
			isTest.set(true);
		}
		if (!isInit.get()) {
			if(isTest.get()) {
				context = new FileSystemXmlApplicationContext(
						"classpath:sys-test-application.xml");
				System.out.println("init test application.xml");
			} else {
				context = new FileSystemXmlApplicationContext(
						"classpath:sys-application.xml");
				System.out.println("init application.xml");
			}
			isInit.set(true);
		}	
	}
	
	public static IBusinessService getIBusinessService() {
		initCofing();
		IBusinessService iBusinessService = (IBusinessService) context.getBean("iBusinessService");
		return iBusinessService;
	}

	
	
	public static IFeedbackService getIFeedbackService() {
		initCofing();	
		IFeedbackService iFeedbackService = (IFeedbackService) context.getBean("iFeedbackService");
		return iFeedbackService;
	}
	
	public static IMemberConfigService getIMemberConfigService() {
		initCofing();
		IMemberConfigService iMemberConfigService = (IMemberConfigService) context.getBean("iMemberConfigService");
		return iMemberConfigService;
	}
	
	
	public static INoticeService getINoticeService() {
		initCofing();
		INoticeService iNoticeService = (INoticeService) context.getBean("iNoticeService");
		return iNoticeService;
	}
	
	
	public static IOperateLogsService getIOperateLogsService() {
		initCofing();	
		IOperateLogsService iOperateLogsService = (IOperateLogsService) context.getBean("iOperateLogsService");
		return iOperateLogsService;
	}
	
	
	public static ISettingService getISettingService() {
		initCofing();
		ISettingService iSettingService = (ISettingService) context.getBean("iSettingService");
		return iSettingService;
	}
	
	public static IAddressService getIAddressService() {
		initCofing();
		IAddressService addressService = (IAddressService) context.getBean("addressService");
		return addressService;
	}

	public static IDocx2PdfService getIDocx2pdfService() {
		initCofing();
		IDocx2PdfService docx2PdfService = (IDocx2PdfService) context.getBean("docx2PdfService");
		return docx2PdfService;
	}

	public static IDigitallySigningPdfService getIDigitallySigningPdfService() {
		initCofing();
		IDigitallySigningPdfService digitallySigningPdfService = (IDigitallySigningPdfService) context.getBean("digitallySigningPdfService");
		return digitallySigningPdfService;
	}

	public static IFileManagementService getIFileManagementService(){
		initCofing();
		IFileManagementService fileManagementService = (IFileManagementService) context.getBean("fileManagementService");
		return fileManagementService;
	}
}
